package com.example.nonacademic_education.config;

import cn.dev33.satoken.stp.StpInterface;
import com.example.nonacademic_education.constant.Constant;
import com.example.nonacademic_education.dao.AdminAuthorityDao;
import com.example.nonacademic_education.dao.AuthorityDao;
import com.example.nonacademic_education.dao.UserDao;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moon
 * @description sa_token权限认证接口实现类
 * @date 2022/6/6
 */
@Component
public class StpInterfaceImpl implements StpInterface {
    @Resource
    private UserDao userDao;
    @Resource
    private AdminAuthorityDao adminAuthorityDao;
    @Resource
    private AuthorityDao authorityDao;
    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 本list仅做模拟，实际项目中要根据具体业务逻辑来查询权限
        List<String> list = authorityDao.getPermissionByIds(adminAuthorityDao.getIdsByUsername(loginId));
        //list.add("get");
        return list;
    }
    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> list = new ArrayList<>();
        list.add(Constant.STUDENT);
        String a=userDao.findAuthorityByUsername(loginId.toString());
        if (a==null){
            return list;
        }
        if (a.equals(Constant.NORMAL_ADMIN)){
            list.add(Constant.NORMAL_ADMIN);
        } else if (a.equals(Constant.SUPER_ADMIN)) {
            list.add(Constant.NORMAL_ADMIN);
            list.add(Constant.SUPER_ADMIN);
        }
        return list;
    }
}
